package com.datang.petstore.controller;

import com.datang.petstore.aop.ForbidAnnotation;
import com.datang.petstore.entity.GetMicroblogListRequest;
import com.datang.petstore.entity.Microblog;
import com.datang.petstore.entity.MicroblogLikeRecord;
import com.datang.petstore.exception.AcceptResultCodeException;
import com.datang.petstore.model.AddMicroblogRequest;
import com.datang.petstore.model.DeleteMicroblogRequest;
import com.datang.petstore.model.GetMicroblogListResponse;
import com.datang.petstore.resultpage.Result;
import com.datang.petstore.resultpage.ResultCode;
import com.datang.petstore.service.MicroblogLikeRecordService;
import com.datang.petstore.service.MicroblogService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/*
 * @auther 顶风少年
 * @mail dfsn19970313@foxmail.com
 * @date 2019-10-30 17:32
 * @notify 微博接口api
 * @version 1.0
 */
@RestController
public class MicroblogController {

    @Autowired
    private MicroblogService microblogService;

    /*
     * @author 顶风少年
     * @date 2019/10/30 17:37
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @param microblog
     * @result com.datang.petstore.utils.Result
     * @notify 添加微博
     * version 1.0
     */
    @PostMapping(value = "microblog")
    @ForbidAnnotation
    public Result addMicroblog(@RequestBody AddMicroblogRequest addMicroblogRequest) {
        Microblog microblog = new Microblog();
        BeanUtils.copyProperties(addMicroblogRequest, microblog);
        microblogService.addMicroblog(microblog);
        return Result.ok();
    }

    /*
     * @author 顶风少年
     * @date 2019/10/30 17:37
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @param microblog
     * @result com.datang.petstore.utils.Result
     * @notify 添加微博
     * version 1.0
     */
    @GetMapping(value = "microblog/list")
    @ForbidAnnotation
    public Result GetMicroblogList(@RequestBody GetMicroblogListRequest getMicroblogListRequest) {
        List<GetMicroblogListResponse> getMicroblogListResponses = microblogService.getMicroblogList(getMicroblogListRequest);
        return Result.ok(getMicroblogListResponses);
    }


    /*
     * @author 顶风少年
     * @date 2019/10/30 17:37
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @param microblog
     * @result com.datang.petstore.utils.Result
     * @notify 微博逻辑删除
     * version 1.0
     */
    @DeleteMapping(value = "microblog")
    @ForbidAnnotation
    public Result delMicroblog(@RequestBody DeleteMicroblogRequest deleteMicroblogRequest) {
        Microblog microblog = microblogService.getMicroblog(deleteMicroblogRequest.getMicroblogId());
        if(microblog.getUserId().equals(deleteMicroblogRequest.getUserId())){
            microblog.setDel("1");
            microblogService.updateMicroblog(microblog);
        }else{
            throw new AcceptResultCodeException(ResultCode.error511);
        }
        return Result.ok();
    }
}

